<?php

namespace app\user\logic;

use app\model\Disinfect;

class DisinfectLogic
{
    protected Disinfect $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new Disinfect();
        $this->factory_id = request()->user_info['factory_id'];
    }

    /**
     * @Description 列表
     * @param array $param
     * @time 2024/03/25 17:51:31
     * @return mixed
     * @author: zj
     */
    public function getList(array $param)
    {
        $query = $this->model->alias('d')
            ->join('fence fe', 'fe.out_id = d.fence_id', 'left')
            ->join('barn b', 'b.out_id = fe.out_barn_id', 'left')
            ->field('d.out_id as id,d.fence_id,fe.name as fence,fe.out_barn_id as barn_id,b.name as barn,d.item_name,d.record_time,d.create_time')
            ->where('d.factory_id', $this->factory_id)
            ->where('d.delete_time', $this->model::IS_DELETE_NO);
        if ($param['barn_id']) {
            $query->where('fe.out_barn_id', $param['barn_id']);
        }
        if ($param['fence_id']) {
            $query->where('d.fence_id', $param['fence_id']);
        }
        if ($param['item_name']) {
            $query->where('d.item_name', $param['item_name']);
        }
        if ($param['record_time_start']) {
            $query->where('record_time', '>=', strtotime($param['record_time_start']));
        }
        if ($param['record_time_end']) {
            $query->where('record_time', '<=', strtotime($param['record_time_end']));
        }

        return $query->order('d.out_id DESC')
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

    /**
     * @Description 下拉选择列表
     * @param array $param
     * @time 2024/03/26 10:28:37
     * @return mixed
     * @author: zj
     */
    public function selectList(array $param)
    {
        $query = $this->model->field('item_name as item_key,item_name')
            ->where('factory_id', $this->factory_id)
            ->where('delete_time', $this->model::IS_DELETE_NO);
        if ($param['item_name']) {
            $query->where('item_name', 'like', '%' . $param['item_name'] . '%');
        }

        return $query->distinct(true)
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

    /**
     * @Description 统计
     * @param array $param
     * @time 2024/03/28 14:44:49
     * @return array
     * @author: zj
     */
    public function statistic(array $param)
    {
        $recordEndTime = $param['record_time_end'] ? strtotime($param['record_time_end']) : request()->time();
        $endTime = strtotime(date('Y-m-d 23:59:59', $recordEndTime));

        $days = 6;

        $data = [];
        for ($i = $days; $i >= 0; $i--) {
            $end_time = $endTime - $i * 86400;
            $start_time = $end_time - 86399;
            $query = $this->model->alias('d')
                ->join('fence f', 'f.out_id = d.fence_id', 'left')
                ->where('d.factory_id', $this->factory_id)
                ->where('d.record_time', '>=', $start_time)
                ->where('d.record_time', '<=', $end_time)
                ->where('d.delete_time', $this->model::IS_DELETE_NO);
            if ($param['barn_id']) {
                $query->where('f.out_barn_id', $param['barn_id']);
            }
            if ($param['fence_id']) {
                $query->where('d.fence_id', $param['fence_id']);
            }
            if ($param['item_name']) {
                $query->where('d.item_name', $param['item_name']);
            }
            $count = $query->count() ?: 0;

            $data[] = [
                'date' => date('Y-m-d', $end_time),
                'count' => $count
            ];
        }

        return $data;
    }

}